package com.home.security.browser.utils;

import com.home.common.constants.LogType;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.MessageUtils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;

/**
 * Description:
 * Created by 王大宸 on 2020-11-12 13:41
 * Created with IntelliJ IDEA.
 */
public class PermissionUtils {

    private static final Logger log = LoggerFactory.getLogger(PermissionUtils.class);

    /**
     * 查看数据的权限
     */
    public static final String VIEW_PERMISSION = "no.view.permission";

    /**
     * 创建数据的权限
     */
    public static final String CREATE_PERMISSION = "no.create.permission";

    /**
     * 修改数据的权限
     */
    public static final String UPDATE_PERMISSION = "no.update.permission";

    /**
     * 删除数据的权限
     */
    public static final String DELETE_PERMISSION = "no.delete.permission";

    /**
     * 导出数据的权限
     */
    public static final String EXPORT_PERMISSION = "no.export.permission";

    /**
     * 其他数据的权限
     */
    public static final String PERMISSION = "no.permission";

    /**
     * 权限错误消息提醒
     *
     * @param permissionsStr 错误信息
     * @return 提示信息
     */
    public static String getMsg(String permissionsStr) {
        String permission = StringUtils.substringBetween(permissionsStr, "[", "]");
        String msg = MessageUtils.subst(PERMISSION, permission);
        if (StringUtils.endsWithIgnoreCase(permission, LogType.SAVE)) {
            msg = MessageUtils.subst(CREATE_PERMISSION, permission);
        } else if (StringUtils.endsWithIgnoreCase(permission, LogType.UPDATE)) {
            msg = MessageUtils.subst(UPDATE_PERMISSION, permission);
        } else if (StringUtils.endsWithIgnoreCase(permission, LogType.REMOVE)) {
            msg = MessageUtils.subst(DELETE_PERMISSION, permission);
        } else if (StringUtils.endsWithIgnoreCase(permission, LogType.EXPORT)) {
            msg = MessageUtils.subst(EXPORT_PERMISSION, permission);
        } else if (StringUtils.endsWithAny(permission,
                new String[]{LogType.VIEW_PERMISSION, LogType.GET_DATA})) {
            msg = MessageUtils.subst(VIEW_PERMISSION, permission);
        }
        return msg;
    }

    /**
     * 返回用户属性值
     *
     * @param property 属性名称
     * @return 用户属性值
     */
    public static Object getPrincipalProperty(String property) {
        Subject subject = SecurityUtils.getSubject();
        if (subject != null) {
            Object principal = subject.getPrincipal();
            try {
                BeanInfo bi = Introspector.getBeanInfo(principal.getClass());
                for (PropertyDescriptor pd : bi.getPropertyDescriptors()) {
                    if (pd.getName().equals(property) == true) {
                        return pd.getReadMethod().invoke(principal, (Object[]) null);
                    }
                }
            } catch (Exception e) {
                log.error("Error reading property [{}] from principal of type [{}]", property,
                        principal.getClass().getName());
            }
        }
        return null;
    }

}
